package com.gitee.wsl.struct.tree.builder

import com.gitee.wsl.struct.tree.MutableNodeElement
import kotlin.jvm.JvmSynthetic


interface ChildDeclarationInterface<T,E> {

    /**
     * This method is used to easily create child in node.
     *
     * ```
     * val root = tree("World") {
     *     child("North America") {
     *         child("USA")
     *     }
     *     child("Europe") {
     *         child("Poland")
     *         child("Germany")
     *     }
     * }
     * ```
     *
     * @return New created TreeNode.
     */
    @JvmSynthetic
    fun child(value: T, childDeclaration: ChildDeclaration<T,E>? = null): E
}

typealias ChildDeclaration<T,E> = ChildDeclarationInterface<T,E>.() -> Unit

/**
 * This method can be used to initialize new tree.
 *
 * ```
 * val root = tree("World") { ... }
 * ```
 *
 * @param root Root element of new tree.
 * @see [ChildDeclarationInterface.child]
 */
@JvmSynthetic
inline fun <reified T> mutableTree(
    root: T,
    //defaultIterator: TreeNodeIteratorType = TreeNodeIteratorType.PreOrder,
    childDeclaration: ChildDeclaration<T,MutableNodeElement<T>>,
): MutableNodeElement<T> {
    val treeNode = MutableNodeElement<T>(root)
    //treeNode.defaultIterator = defaultIterator
    treeNode.childDeclaration()
    return treeNode
}